package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前购物车是否存在该商品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        // 一个user一个购物车，使用动态SQL来查询
        Long userid = BaseContext.getCurrentId();
        shoppingCart.setUserId(userid);
        List<ShoppingCart> list =  shoppingCartMapper.addShoppingCart(shoppingCart);
        //如果存在，只需要将数量加一
        if (list != null && list.size()>0){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        }else {
            //如果不存在，添加到购物车
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {
                //添加的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setNumber(1);
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
            } else {

            }
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 回显购物车
     * @return
     */
    @Override
    public List<ShoppingCart> getShoppingCart() {
        // 首先获取当前用户的id
        Long currentId = BaseContext.getCurrentId();
        return shoppingCartMapper.getShoppingCart(currentId);
    }

    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 获取userid，删除的应该是当前登录用户的购物车里的商品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        // 获取到当前用户的购物车数据
        List<ShoppingCart> list = shoppingCartMapper.addShoppingCart(shoppingCart);
        // 如果存在只需要将数量-1
        if (list != null && list.size() >0) {
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.updateNumberById(cart);
            if (cart.getNumber() == 0){
                shoppingCartMapper.delete(cart);
            }
        }


    }

    /**
     * 清空购物车
     */
    @Override
    public void delAllShoppingCart() {
        shoppingCartMapper.delAllShoppingCart(BaseContext.getCurrentId());
    }
}
